﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Eaktest;
using SystemCore;

namespace EakTest
{
    public partial class MasterForm : Form
    {
        protected ScreenMode m_eScreenMode = ScreenMode.Idle;

        public MasterForm()
        {
            InitializeComponent();

            tsStatusName.Text = UserContext.FullName;
            tsStatusRole.Text = UserContext.Role.ToString(); ;
        }

        private void tsExit_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void tsAdd_Click(object sender, EventArgs e)
        {
            Enter_ScreenMode(ScreenMode.Add);
        }

        private void tsEdit_Click(object sender, EventArgs e)
        {
            Enter_ScreenMode(ScreenMode.Edit);
        }

        private void tsDelete_Click(object sender, EventArgs e)
        {
            Enter_ScreenMode(ScreenMode.Delete);
        }

        private void tsSave_Click(object sender, EventArgs e)
        {
            Save_ScreenMode(m_eScreenMode);
        }

        private void tsCancel_Click(object sender, EventArgs e)
        {
            Cancel_ScreenMode(m_eScreenMode);
        }

        #region Virtual method

        public virtual void Enter_ScreenMode(ScreenMode mode)
        {
            switch (mode)
            {
                case ScreenMode.Idle:
                    this.m_eScreenMode = mode;
                    EnableButton_IdleMode();
                    Enter_IdleMode();
                    break;
                case ScreenMode.View:
                    this.m_eScreenMode = mode;
                    EnableButton_ViewMode();
                    Enter_ViewMode();
                    break;
                case ScreenMode.Add:
                    this.m_eScreenMode = mode;
                    EnableButton_AddMode();
                    Enter_AddMode();
                    break;
                case ScreenMode.Edit:
                    this.m_eScreenMode = mode;
                    EnableButton_EditMode();
                    Enter_EditMode();
                    break;
                case ScreenMode.Delete:
                    this.m_eScreenMode = mode;
                    EnableButton_DeleteMode();
                    Enter_DeleteMode();
                    Save_ScreenMode(ScreenMode.Delete);
                    break;
            }
        }
        public virtual void Save_ScreenMode(ScreenMode mode)
        {
            switch (mode)
            {
                case ScreenMode.Add:
                    #region Add

                    if (MessageDialog.Confirmation("Do you want to add?"))
                    {
                        try
                        {
                            if (Save_AddMode())
                            {
                                MessageDialog.Information("Process Completed.");
                                Enter_ScreenMode(ScreenMode.Idle);
                            }
                            else
                            {
                                MessageDialog.Warning("Terminate due to an error.");
                            }

                        }
                        catch (Exception ex)
                        {
                            MessageDialog.Error(ex.ToString());
                        }
                    }
                    else
                    {
                        Cancel_AddMode();
                        Enter_ScreenMode(ScreenMode.Idle);
                    }

                    #endregion
                    break;
                case ScreenMode.Edit:
                    #region Edit

                    if (MessageDialog.Confirmation("Do you want to update?"))
                    {
                        try
                        {
                            if (Save_EditMode())
                            {
                                MessageDialog.Information("Process Completed.");
                                Enter_ScreenMode(ScreenMode.View);
                            }
                            else
                            {
                                MessageDialog.Warning("Terminate due to an error.");
                            }
                        }
                        catch (Exception ex)
                        {
                            MessageDialog.Error(ex.ToString());
                        }
                    }
                    else
                    {
                        Cancel_EditMode();
                        Enter_ScreenMode(ScreenMode.Idle);
                    }

                    #endregion
                    break;
                case ScreenMode.Delete:
                    #region Delete

                    if (MessageDialog.Confirmation("Do you want to delete?"))
                    {
                        try
                        {
                            if (Save_DeleteMode())
                            {
                                MessageDialog.Information("Process Completed.");
                                Enter_ScreenMode(ScreenMode.Idle);
                            }
                            else
                            {
                                MessageDialog.Warning("Terminate due to an error.");
                            }
                        }
                        catch (Exception ex)
                        {
                            MessageDialog.Error(ex.ToString());
                        }
                    }
                    else
                    {
                        Cancel_DeleteMode();
                        Enter_ScreenMode(ScreenMode.Idle);
                    }

                    #endregion
                    break;
            }
        }
        public virtual void Cancel_ScreenMode(ScreenMode mode)
        {
            switch (mode)
            {
                case ScreenMode.View:
                    Cancel_ViewMode();
                    Enter_ScreenMode(ScreenMode.Idle);
                    break;
                case ScreenMode.Add:
                    if (MessageDialog.Confirmation("Do you want to cancel?"))
                    {
                        try
                        {
                            Cancel_AddMode();
                            Enter_ScreenMode(ScreenMode.Idle);
                        }
                        catch (Exception ex)
                        {
                            MessageDialog.Error(ex.ToString());
                        }
                    }
                    break;
                case ScreenMode.Edit:
                    if (MessageDialog.Confirmation("Do you want to cancel?"))
                    {
                        try
                        {
                            Cancel_EditMode();
                            Enter_ScreenMode(ScreenMode.View);
                        }
                        catch (Exception ex)
                        {
                            MessageDialog.Error(ex.ToString());
                        }
                    }

                    break;
                case ScreenMode.Delete:
                    if (MessageDialog.Confirmation("Do you want to cancel?"))
                    {
                        try
                        {
                            Cancel_DeleteMode();
                            Enter_ScreenMode(ScreenMode.View);
                        }
                        catch (Exception ex)
                        {
                            MessageDialog.Error(ex.ToString());
                        }
                    }
                    break;
            }
        }

        #region Virtual Method : Must Implement by inherited form

        public virtual void Enter_IdleMode() { }
        public virtual void Enter_ViewMode() { }
        public virtual void Enter_AddMode() { }
        public virtual void Enter_EditMode() { }
        public virtual void Enter_DeleteMode() { }

        public virtual bool Save_AddMode() { return true; }
        public virtual bool Save_EditMode() { return true; }
        public virtual bool Save_DeleteMode() { return true; }

        public virtual void Cancel_ViewMode() { }
        public virtual void Cancel_AddMode() { }
        public virtual void Cancel_EditMode() { }
        public virtual void Cancel_DeleteMode() { }

        #endregion

        #region Virtual Method : Enable/Disable Button when change ScreenMode

        public virtual void EnableButton_IdleMode()
        {
            ControlUtility.EnabledControl(true, tsAdd, tsExit);
            ControlUtility.EnabledControl(false, tsEdit, tsDelete, tsSave, tsCancel);
        }

        public virtual void EnableButton_ViewMode()
        {
            ControlUtility.EnabledControl(true, tsAdd, tsEdit, tsDelete, tsExit);
            ControlUtility.EnabledControl(false, tsSave, tsCancel);
        }

        public virtual void EnableButton_AddMode()
        {
            ControlUtility.EnabledControl(true, tsSave, tsCancel);
            ControlUtility.EnabledControl(false, tsAdd, tsEdit, tsDelete, tsExit);
        }

        public virtual void EnableButton_EditMode()
        {
            ControlUtility.EnabledControl(true, tsSave, tsCancel);
            ControlUtility.EnabledControl(false, tsAdd, tsEdit, tsDelete, tsExit);
        }

        public virtual void EnableButton_DeleteMode()
        {
            ControlUtility.EnabledControl(false, tsSave, tsCancel);
            ControlUtility.EnabledControl(false, tsAdd, tsEdit, tsDelete, tsExit);
        }

        #endregion

        #endregion


    }
}
